$(function() {
  var socket = io()
  var sendInput = $('#send-input')
  var sendButton = $('#send-btn')

  var checkInput = function() {
    if(sendInput.val() == "") {
      sendButton.addClass("disabled")
    }
    else {
      sendButton.removeClass("disabled")
    }
  }
  checkInput()

  sendInput.keyup(function(event) {
    checkInput()
    if (event.which == 13) {
      sendButton.click()
    }
  })

  sendButton.click(function(event) {
    if (!sendButton.hasClass("disabled")) {
      socket.emit('send message', sendInput.val())
      sendInput.val("").focus()
      checkInput()
    }
  })

  socket.on('send message', function(msg) {
    var msgBar = $("<div></div>").addClass("alert alert-info").html(msg)
    $('#chat-list').append(msgBar)
    $('#chat-area').scrollTop($('#chat-list').height())
  })
})
